package com.example.demo.controller;

import com.example.demo.domain.entity.Person;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;

@RestController
public class DemoController {


    /**
     * 国际化取消息用的组件
     */
    @Resource
    private MessageSource messageSource;

    @GetMapping("/haha")
    public String haha(HttpServletRequest request){

        Locale locale = request.getLocale();
        //利用代码的方式获取国际化配置文件中指定的配置项的值
        return messageSource.getMessage("login", null, locale);
    }

    @GetMapping("/hello")
    public String hello(){
        return "Hello World";
    }

    @GetMapping("person")
    public Person getPerson(@RequestParam(value = "userName") String userName){
        Person person = new Person();
        person.setId(1L);
        person.setUserName(userName);
        return person;
    }
 }
